// APP
// Main.html第一次点击对应Tab时候初始化页面
//if (window.plus) {
//    ready();
//} else {
//    document.addEventListener('plusready', ready, false);
//}

// Web
$(function () {
    if (AGENT_CLIENT.WEB) {
        ready();
    } else if (AGENT_CLIENT.APP) {
        $(".main").css("padding-bottom", "2px");
    }
});
// 初始化方法
function ready() {
    var classificationPage = function () {
        this.init();
    };
    classificationPage.prototype = {
        // 初始化页面
        init: function () {
            this.loadData();
            this.bindEvent();
        },

        // 分类数据
        categorydata: {"typelist": [], "subtypelist": {}},

        // 绑定事件
        bindEvent: function () {
            var model = this;
            // 搜索
            $("#searchinput").on("tap", function () {
                showSearchPage();
            });

            // 分类切换
            $("#typelist").on("tap", "li", function () {
                var tip = $(this).attr("tip");
                var activeli = $('#typelist li[class=active]');
                if (activeli.attr("tip") == tip) {
                    return;
                }
                activeli.removeClass("active")
                $(this).addClass("active");
                var top = ($(this).offset().top - CONFIG_MANAGER.headerHight);
                var movedtop = $("#leftpart").scrollTop() + top;
                $("#leftpart").animate({scrollTop: movedtop}, 300);

                model.paintright(parseInt(tip));
            });

            // 点击分类
            $("#subtypelist").on("tap", "li", function () {
                redirect("../goods/goodslist.html?catalog=" + $(this).attr("tip"));

            });
        },

        // 加载数据
        loadData: function () {
            var model = this;

            // 读取缓存(1天有效)
            var nowtime = new Date(new Date().Format("yyyy-MM-dd")).getTime();
            var categorycachedata = getLocalStorage(STORAGE_MANAGER.category);
            if (categorycachedata && categorycachedata.time >= nowtime) {
                this.categorydata = categorycachedata.data;
                this.paintPage();
                return;
            }

            $.ajax({
                type: "get",
                url: CONFIG_MANAGER.RequstUrl + "v1/basic/category/mobile",
                beforeSend: function () {
                    $.showWaiting();
                }
            }).done(function (data) {
                for (var i = 0; i < data.length; i++) {
                    var typeitem = data[i];
                    model.categorydata.typelist.push({id: typeitem.id, name: typeitem.name, code: typeitem.code});
                    model.categorydata.subtypelist[typeitem.id] = [];
                    for (var j = 0; j < typeitem.child.length; j++) {
                        var subtypeitem = typeitem.child[j];
                        model.categorydata.subtypelist[typeitem.id].push({
                            id: subtypeitem.id,
                            name: subtypeitem.name,
                            code: subtypeitem.code,
                            icon: subtypeitem.icon
                        });
                    }
                }
                model.paintPage();

                setLocalStorage(STORAGE_MANAGER.category, {time: nowtime, data: model.categorydata});
            }).fail(function () {
                // 失败
            }).always(function () {
                // 不论失败成功都执行
                $.closeWaiting();
            });
        },

        // 画页面
        paintPage: function () {
            var activetypeid = this.categorydata.typelist[0].id;
            this.paintleft();
            this.paintright(activetypeid);
            // 懒加载
            $('img.lazy').lazyload({
                placeholder: "../../images/white.gif",
                container: $("#rightpart"),
                event: "scroll",
                threshold: 100,
                failure_limit: 10
            });
        },

        // 左边分类
        paintleft: function () {
            var data = this.categorydata.typelist;
            var html = [];
            for (var i = 0; i < data.length; i++) {
                var item = data[i];
                var classname = (i == 0) ? 'active' : "";
                html.push("<li tip='" + item.id + "' class='" + classname + "'>" + item.name + "</li>");
            }

            $("#typelist").html(html.join(""));
        },

        // 子分类
        paintright: function (typeid) {
            var data = this.categorydata.subtypelist[typeid];
            var html = [];
            for (var i = 0; i < data.length; i++) {
                var item = data[i];
                html.push("<li tip=" + item.code + "><div class='pic'>");
                html.push("<img class='lazy'  src='../../images/place/150x150.png' data-original='" + CONFIG_MANAGER.imgeUrl + item.icon + "' /><p>" + item.name + "</p>");
                html.push("</div></li>");
            }

            $("#subtypelist").html(html.join(""));
            $('img.lazy').lazyload({placeholder: "../../images/white.gif", threshold: 100, failure_limit: 10});
        }
    };

    var _classificationPage = new classificationPage();
}

// 关闭搜索页
function showSearchPage() {
    $("#searchiframe").show();
    searchiframe.window.show();
    searchiframe.window.loadSearchHistory();
}

// 关闭搜索页
function closeSearchPage() {
    $("#searchiframe").hide();
}